package com.zengqingfa.designpattern.create.singleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

/**
 *
 * @fileName: Singleton6
 * @author: zengqf3
 * @date: 2021-4-5 21:00
 * @description: 枚举
 */
public enum Singleton6 {

    INSTANCE;

    public static void main(String[] args) throws Exception {
        Singleton6 singleton1 = INSTANCE;
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton6_obj"));
        oos.writeObject(singleton1);
        oos.flush();
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("singleton6_obj"));
        Singleton6 singleton2 = (Singleton6) ois.readObject();
        ois.close();
        //判断通过序列化后的对象是否相等
        System.out.println(singleton1 == singleton2);

        Constructor<Singleton6> constructor = Singleton6.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        /**
         * Exception in thread "main" java.lang.NoSuchMethodException:
         * com.zengqingfa.designpattern.create.singleton.Singleton6.<init>()
         */
        Singleton6 singleton6 = constructor.newInstance();
    }
}
